package com.yl.ydk.presenter.impl

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.yl.ydk.R
import com.yl.ydk.api.Api
import com.yl.ydk.base.BasePresenter
import com.yl.ydk.bean.DiseaseSearchBean
import com.yl.ydk.presenter.contract.DiseaseSearchActivityContract
import com.yl.ydk.ui.activity.DiseaseDetailActivity
import com.yl.ydk.ui.adapter.DiseaseSearchAdapter
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class DiseaseSearchActivityImpl : BasePresenter<DiseaseSearchActivityContract.View>(),
    DiseaseSearchActivityContract.Presenter<DiseaseSearchActivityContract.View> {

    override fun getGetDiseaseList(context: Context, vararg query: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getGetDiseaseList(*query)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<DiseaseSearchBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    override fun onNext(t: DiseaseSearchBean?) {
                        dismissLoadingDialog()
                        if (t?.data != null && t.code == 200) {
                            mView?.getllNormal()?.let {
                                it.visibility = View.GONE
                            }
                            mView?.getRvSearch()?.let { it ->
                                it.visibility = View.VISIBLE
                                val adapter = DiseaseSearchAdapter(t.data.list.toMutableList())
                                it.adapter = adapter
                                val manager = LinearLayoutManager(context)
                                it.layoutManager = manager

                                adapter.onListItemClick(object :
                                    DiseaseSearchAdapter.onListItemClick {
                                    override fun onListItemClick(item: DiseaseSearchBean.DataX?) {
                                        mView?.getActivity()?.let { activity ->
                                            val intent = Intent(activity, DiseaseDetailActivity::class.java)
                                            intent.putExtra("data", item?.name)
                                            activity.startActivityIn(intent, activity)
                                            activity.finish()
                                        }
                                    }
                                })
                            }
                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }
}